﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using System.IO;

namespace XJLSC.PM
{
    public partial class SCJHDCCX : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        public SCJHDCCX()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
            string x1 = "";
            string x2 = "";
            string x3 = "";

            x1 = dateTimePicker1.Value.ToShortDateString();
            x2 = dateTimePicker2.Value.ToShortDateString();
            x3 = textBox1.Text.ToString();
            FMCHAXUN xx = new FMCHAXUN(x1, x2, x3);
            SCJHDCDY f = new SCJHDCDY();
            f.cdgzzx += new SCJHDCDY.Cdgzzx(textBox1_TextChanged);
            f.Show();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            int j = 0;
            int k = rowMergeView1.Columns.Count - 1;
            if (rowMergeView1.Columns.Count - 1 > j)
            {
                for (int i = j; i <= k; i++)
                {

                    rowMergeView1.Columns.RemoveAt(j);
                }
            }
                 
            string strSql = "";

            strSql = "SELECT DISTINCT RTRIM(TA006) 产品品号,RTRIM(TA034) + ' ' + RTRIM(TA035) 品名规格,RTRIM(INVMB.UDF01) 老编码,TA001 工单单别,TA002 工单单号,ISNULL(MW002,'') 工艺名称 FROM V_MOCTY LEFT JOIN MOCTA ON TY001=TA001 AND TY002=TA002 LEFT JOIN CMSMW ON TY019=MW001 LEFT JOIN INVMB ON TA006=MB001 WHERE TY003>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) AND TY003<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker2.Value.ToShortDateString() + "'),112) AND (TA021='" + textBox1.Text + "' OR '" + textBox1.Text + "'='') AND (MW002='" + comboBox1.Text.ToString() + "' OR '" + comboBox1.Text.ToString() + "'='') ORDER BY RTRIM(TA006)";   

            
            //textBox6.Text = strSql;
            //MessageBox.Show(strSql,"提示");
            try
            {
                DataTable dt;
                this.rowMergeView1.DataSource = "";
                rowMergeView1.Refresh();

                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                dt.Rows.Add();
                rowMergeView1.DataSource = dt;
                this.rowMergeView1.ColumnHeadersHeight = 80;

                //foreach (DataGridViewColumn dr in this.rowMergeView1.Columns)
                //{
                //    if (dr.Index > this.rowMergeView1.Columns["预计产量"].Index)
                //    {
                //        dr.Width = 30;
                //    }

                //}
                //this.dataGridView1.DefaultCellStyle.BackColor = Color.Yellow;
               // this.rowMergeView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
                this.rowMergeView1.AllowUserToAddRows = false;
               this.rowMergeView1.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;
               this.rowMergeView1.Columns["产品品号"].Width = 80;
               this.rowMergeView1.Columns["品名规格"].Width = 200;
               this.rowMergeView1.Columns["工单单别"].Width = 50;
               this.rowMergeView1.Columns["工单单号"].Width = 80;
               this.rowMergeView1.Columns["工艺名称"].Width = 50;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
            try
            {
                for (DateTime i = dateTimePicker1.Value; i <= dateTimePicker2.Value; i = i.AddDays(1))
                {

                    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                    DataGridViewTextBoxColumn acCode1 = new DataGridViewTextBoxColumn();
                    string xh = "";
                    string month = "";
                    string day = "";
                    month = "0" + i.Month.ToString();
                    month = month.Substring(month.Length - 2, 2);
                    day = "0" + i.Day.ToString();
                    day = day.Substring(day.Length - 2, 2);
                    xh = i.Year.ToString() + month + day;
                    
                    //xh = xh.Substring(xh.Length - 2, 2);
                    acCode.Name = xh + "计划";
                    acCode.DataPropertyName = xh + "计划";
                    acCode.HeaderText = "计划";
                    rowMergeView1.Columns.Add(acCode);

                    acCode1.Name = xh + "完成";
                    acCode1.DataPropertyName = xh + "完成";
                    acCode1.HeaderText = "完成";
                    rowMergeView1.Columns.Add(acCode1);
                    this.rowMergeView1.ColumnHeadersHeight = 40;
                    this.rowMergeView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
                    this.rowMergeView1.MergeColumnNames.Add("产品品号");
                    this.rowMergeView1.MergeColumnNames.Add("品名规格");
                    this.rowMergeView1.AddSpanHeader(this.rowMergeView1.Columns.Count - 2, 2, xh);
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
            string gddb = "";
            string gddh = "";
            string pcrq = "";
            string sjlx = "";
            progressBar1.Visible = true;
            progressBar1.Maximum = rowMergeView1.Rows.Count;//设置最大长度值
            progressBar1.Value = 0;//设置当前值
            progressBar1.Step = 1;//设置没次增长多少
            for (int i = 0; i <= this.rowMergeView1.Rows.Count - 2; i++)
            {
                progressBar1.Value += progressBar1.Step;
                for (int h = this.rowMergeView1.Columns["工艺名称"].Index + 1; h <= this.rowMergeView1.Columns.Count - 1; h++)
                {
                    gddb = (this.rowMergeView1.Rows[i].Cells[this.rowMergeView1.Columns["工单单别"].Index].Value == null) ? "" : this.rowMergeView1.Rows[i].Cells[this.rowMergeView1.Columns["工单单别"].Index].Value.ToString();
                    gddh = (this.rowMergeView1.Rows[i].Cells[this.rowMergeView1.Columns["工单单号"].Index].Value == null) ? "" : this.rowMergeView1.Rows[i].Cells[this.rowMergeView1.Columns["工单单号"].Index].Value.ToString();
                    pcrq = this.rowMergeView1.Columns[h].Name.ToString().Substring(0, 8);
                    sjlx = this.rowMergeView1.Columns[h].Name.ToString().Substring(8, 2);
                    strSql = "SELECT ISNULL(TY006,0) TY006 FROM V_MOCTY WHERE TY001='" + gddb + "' AND TY002='" + gddh + "' AND TY003='" + pcrq + "' AND LX='" + sjlx + "'";
                    //MessageBox.Show(this.rowMergeView1.Columns[h].Name.ToString(),"提示");
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            //MessageBox.Show(sdr1["TY006"].ToString(), "提示");
                            this.rowMergeView1.Rows[i].Cells[h].Value = Convert.ToDouble(sdr1["TY006"]).ToString(); ;

                        }

                        if (sdr1.HasRows == false)
                        {
                            this.rowMergeView1.Rows[i].Cells[h].Value = "0"; 
                        }

                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }
                    finally
                    {
                        sdr1.Close();
                    }
                }
            }
            progressBar1.Visible = false;

            this.rowMergeView1.Rows[this.rowMergeView1.Rows.Count - 1].Cells[3].Value = "合计";
            this.rowMergeView1.Rows[this.rowMergeView1.Rows.Count - 1].DefaultCellStyle.BackColor = Color.Yellow;
            for (int i = this.rowMergeView1.Columns["工艺名称"].Index + 1; i <= rowMergeView1.Columns.Count - 1; i++)
            {
                double sum2 = 0;
                for (int l = 0; l < this.rowMergeView1.Rows.Count - 1; l++)
                {
                    sum2 += double.Parse(this.rowMergeView1.Rows[l].Cells[i].Value==null ? "0":this.rowMergeView1.Rows[l].Cells[i].Value.ToString());
                }
                this.rowMergeView1.Rows[this.rowMergeView1.Rows.Count - 1].Cells[i].Value = sum2;
            }
        }

        private void rowMergeView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show(this.rowMergeView1.Columns[this.rowMergeView1.CurrentCell.ColumnIndex].Name.ToString(),"提示");
        }

        private void rowMergeView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0 || rowMergeView1.Rows.Count == 0)
                return;

            if (lastRow.Count <= 0)
            {
                int index = rowMergeView1.Rows.Count - 1;
                lastRow.Add(rowMergeView1.Rows[index]);
                rowMergeView1.Rows.RemoveAt(index);
            }
        }
        private List<DataGridViewRow> lastRow = new List<DataGridViewRow>();
        private void lwolf_Sorted(object sender, EventArgs e)
        {
            if (lastRow.Count <= 0)
                return;

            rowMergeView1.Rows.Add(lastRow[0]);
            lastRow.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string columnTitle = null;
                try
                {
                    //写入列标题    
                    for (int i = 0; i < rowMergeView1.ColumnCount; i++)
                    {
                        if (rowMergeView1.Columns[i].Visible == true)
                        {
                            if (i > 0)
                            {
                                columnTitle += "\t";
                            }
                            columnTitle += rowMergeView1.Columns[i].HeaderText;

                        }

                    }
                    sw.WriteLine(columnTitle);
                    //写入列内容    
                    for (int j = 0; j < rowMergeView1.Rows.Count; j++)
                    {

                        string columnValue = null;
                        for (int k = 0; k < rowMergeView1.Columns.Count; k++)
                        {
                            if (rowMergeView1.Columns[k].Visible == true)
                            {
                                if (k > 0)
                                {
                                    columnValue += "\t";
                                }
                                if (rowMergeView1.Rows[j].Cells[k].Value == null)
                                    columnValue += "";
                                else
                                    columnValue += rowMergeView1.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }

        
    }
}
